﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core;
using Core.DAL;

namespace xManagerWebApp.__Membership
{
    public partial class WF_ManageProfile : System.Web.UI.Page
    {
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            Profile p;

            using (DataAccessLayer dc = new DataAccessLayer())
            {
                var query = from pfile in dc.DataContext.Profiles
                            where pfile.Id.Equals(Core.User.CurrentUserId.Value)
                            select pfile;

                p = query.SingleOrDefault();
            }

            if (p != null)
            {
                legend.Text = "Editing profile";
                this.FullName.Text = p.FullName;
                this.Adress.Text = p.Adress;
                this.NIB.Text = p.NIB;
                this.Homephone.Text = "" + p.Homephone;
                this.Cellphone.Text = "" + p.Cellphone;
            }
            

        }

        protected void save_profile(object sender, EventArgs e)
        {
            using (DataAccessLayer dc = new DataAccessLayer())
            {                
                Profile prfile = dc.DataContext.Profiles.SingleOrDefault(px => px.Id.Equals(Core.User.CurrentUserId.Value));

                if (this.FullName.Text != null) prfile.FullName = this.FullName.Text;
                if (this.Adress.Text != null) prfile.Adress = this.Adress.Text;
                if (this.NIB.Text != null) prfile.NIB = this.NIB.Text;
                if (this.Homephone.Text != null) prfile.Homephone = this.Homephone.Text;
                if (this.Cellphone.Text != null) prfile.Cellphone = this.Cellphone.Text;
                
                dc.DataContext.SubmitChanges();
                Server.Transfer(".\\WF_ViewProfile.aspx");
            }
        }
    }
}
